import libtcodpy as libtcod

class ImageError(Exception):
    def __init__(self, value):
        self.value = value
    def __str__(self):
        return repr(self.value)

class Image(object):
    def __init__(self, img=None, width=None, height=None):
        if img is not None:
            print (width is None) and (height is None), width, height
            assert (width is None) and (height is None)
            self._img = None
        elif width is not None:
            if height is None:
                raise ImageError("Image should specify both width and height or neither")
            self._img = libtcod.image_new(width, height)
        elif height is not None:
            raise ImageError("Image should specify both width and height or neither")
        else:
            self._img = None

    # XXX: Check what the libtcodpy defaults are for these...
    def blit(self, con, x, y, bkgnd_flag, scalex, scaley, angle):
        libtcod.image_blit(self._img, con.console, x, y, bkgnd_flag, scalex, scaley, angle)
    def blit_rect(self, image, con, x, y, w, h, bkgnd_flag):
        libtcod.image_blit_rect(self._img, con.console, x, y, w, h, bkgnd_flag)
    def blit_2x(self, dest, dx, dy, sx=0, sy=0, w=-1, h=-1):
        libtcod.image_blit_2x(self._img, dest.console, dx, dy, sx, sy, w, h)

    # I'm not using this, but check it out anyway.  Could it be a @property?
    def set_key_color(self, keyColor):
        libtcod.image_set_key_color(image, keyColor)

    def load(self, filename):
        self._img = libtcod.image_load(filename)

    def save(self, filename):
        libtcod.image_save(self._img, filename)

    def from_console(self, con):
        self.img = libtcod.image_from_console(con)
    def refresh_console(self, con):
        libtcod.image_refresh_console(self._im, con)

    def clear(self, color):
        libtcod.image_clear(self._im, color)

    def get_size(self):
        return size

    def get_pixel(self, x, y):
        return libtcod.image_get_pixel(self._img, x, y)

    def get_alpha(self, x, y):
        return libtcod.image_get_alpha(self._img, x, y)

    def is_pixel_transparent(self, x, y):
        return libtcod.image_is_pixel_transparent(self._img, x, y)

    def get_mipmap_pixel(self, x0, y0, x1, y1):
        return libtcod.image_get_mipmap_pixel(self._img, x0, y0, x1, y1)

    def put_pixel(self, x, y, col):
        return libtcod.image_put_pixel(self._img, x, y, col)

    def scale(self, neww, newh):
        libtcod.image_scale(self._img, neww, newh)

    def hflip(self):
        libtcod.image_hflip(self._img)

    def vflip(self):
        libtcod.image_vflip(self._img)

    def invert(self):
        libtcod.image_imvert(self._img)

    @property
    def size(self):
        return libtcod.image_get_size(self._img)
    @property
    def width(self):
        return libtcod.image_get_size(self._img)[0]
    @property
    def height(self):
        return libtcod.image_get_size(self._img)[1]






